package com.walle.sonar.export.report.reportdoc.module.severity;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.walle.ak47.commons.utils.json.GBuilder;
import com.walle.sonar.export.report.reportdoc.module.issues.IssueItemInfo;
import com.walle.sonar.export.report.reportdoc.module.issues.Issueser;


public class Severityer {
	
	public static Map<String,SeverityItemInfo> getSeverityStatistics() {
		
		Map<String, IssueItemInfo> issues = Issueser.getIssues();
		int issueTotal = issues.size() ;
		
		Map<String,SeverityItemInfo> severity_num = new HashMap<String, SeverityItemInfo>() ;
		severity_num.put("BLOCKER", new SeverityItemInfo()) ;   // 阻断块。最严重的错误类型；
		severity_num.put("CRITICAL", new SeverityItemInfo()) ;	// 严重的错误类型；
		severity_num.put("MAJOR", new SeverityItemInfo()) ;		// 重要的错误类型；
		severity_num.put("MINOR", new SeverityItemInfo()) ;		// 次要的错误类型；
		severity_num.put("INFO", new SeverityItemInfo()) ;		// 一般信息；

		// 循环按严重程度进行统计次数
		issues.values().forEach(issueItemInfo->{
			
			String severity = issueItemInfo.getSeverity();
			SeverityItemInfo severityItemInfo = severity_num.get( severity );
			if ( severityItemInfo==null)
				severity_num.put(severity, new SeverityItemInfo());
			
			severity_num.get( severity ).getNum().getAndIncrement() ;
		});
		
		// 根据serverity_num统计占比
		severity_num.forEach((serverity,severityItemInfo)->{
			BigDecimal num = new BigDecimal(severityItemInfo.getNum().doubleValue()) ;
			BigDecimal percent = num.divide( new BigDecimal(issueTotal), 2, RoundingMode.HALF_UP ).multiply(new BigDecimal(100))  ;
			severityItemInfo.setPercent(percent+"");
		});
		
		severity_num.put("issueTotal", new SeverityItemInfo(null, new AtomicInteger(issueTotal), null) ) ;
		
		return severity_num;
	}
	
	public static void main(String[] args) {
		Map<String, SeverityItemInfo> severityStatistics = Severityer.getSeverityStatistics();
		String severityStatisticesJson = GBuilder.toJson(severityStatistics);
		System.err.println( severityStatisticesJson );
		
	}
}
